// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class test extends cc.Component {
    public idle() {
        this.clear();
        let t = .45;

        let x1 = cc.tween().by(t, { scaleX: -0.05 }, { easing: 'sineOut' });
        let x2 = cc.tween().by(t, { scaleX: 0.05 }, { easing: 'sineIn' });
        let tween = cc.tween().sequence(x1, x2)
        let rep = cc.tween().repeatForever(tween);
        cc.tween(this._n).then(rep).start()

        let y1 = cc.tween().by(t, { scaleY: -0.06 }, { easing: 'sineOut' });
        let y2 = cc.tween().by(t, { scaleY: 0.06 }, { easing: 'sineIn' });
        let tween1 = cc.tween().sequence(y1, y2)
        let rep1 = cc.tween().repeatForever(tween1);

        cc.tween(this._n).delay(t).call(() => {
            cc.tween(this._n).then(rep1).start()
        }).start();
    }

    public press() {
        this.clear();
        let t = .55;
        this.graySpr(true)
        cc.tween(this._n)
            .to(t, { scale: 0.8 }, { easing: 'bounceOut' })
            .start();
        
    }

    graySpr(b: boolean) {
        this._spr.setMaterial(0, this._spr.getMaterials()[b ? 2 : 1]);
    }

    public pop() {
        this.clear();
        let t = .55;
        this.graySpr(false)
        this._n.scale = 0.8;
        cc.tween(this._n)
            .to(t, { scale: 1 }, { easing: 'bounceOut' })
            .start();
    }

    private clear() {
        cc.Tween.stopAllByTarget(this._n);
        this._n.scaleX = this._n.scaleY = 1;
        this.graySpr(false)
    }

    _spr: cc.Sprite;
    _n: cc.Node;
    protected onEnable(): void {
        this._spr = this.node.getChildByName('spr').getComponent(cc.Sprite);
        this._n = this._spr.node;
        this.idle();
    }
}
